import collections


class Solution(object):
    def findShortestSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        c = collections.Counter(nums)
        d = max(c.values())
        if d == 1:
            return 1
        n = len(nums)
        start = {}
        end = {}
        min_len = n
        now_set = set()
        for key in c:
            if c[key] == d:
                now_set.add(key)
        for i in range(n):
            if nums[i] in now_set:
                if nums[i] not in start:
                    start[nums[i]] = i
                else:
                    end[nums[i]] = i
        for key in now_set:
            min_len = min(min_len, end[key] - start[key] + 1)
        return min_len
